package com.ag.wzp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbUser;
import com.ag.common.IPermissionService;
import com.ag.common.PageUtil;
import com.ag.wzp.service.impl.QueryService;

/**
 * 事件查询action
 * @author 温镇澎
 *
 */
@Scope("prototype")
@Controller("queryAction")
public class QueryAction extends com.ag.common.BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(QueryAction.class);
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;
	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}
	@Resource(name="queryService")
	private QueryService queryService;
	private Map<String,String> queryOption;
	private PageUtil pageUtil;
	private String ajaxRst;
	/**
	 * Ljh 权限判断
	 */
	private String currentUid;
	private List<String> ids;
	
	public String getCurrentUid() {
		return currentUid;
	}

	public void setCurrentUid(String currentUid) {
		this.currentUid = currentUid;
	}

	public String query() {
		logger.info("queryAction");
		pageUtil = queryService.query(queryOption);
		TbUser user = null;
		try {
			user = pms.getCurrentUser(sessionMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setCurrentUid(user.getUId());
		ids = queryService.getEidByUid(currentUid);
//		ids = new ArrayList<String>();
//		ids.add("1010241458");
//		ids.add("1010241459");
		if(pageUtil==null)
			ajaxRst = "false";
		else
			ajaxRst = "true";
		logger.info(ajaxRst);
		return "queryOK";
	}
	
	public String queryByUser(){
		logger.info("queryByUser");
		TbUser user = null;
		try {
			user = pms.getCurrentUser(sessionMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (user != null) {
			if(!user.getUId().equals("jpagg"))
				queryOption.put("UId", user.getUId());
			long start = System.currentTimeMillis();
			pageUtil = queryService.query(queryOption);
			long end = System.currentTimeMillis();
			long time = end - start;
			this.setCurrentUid(user.getUId());
			ids = queryService.getEidByUid(currentUid);
//			ids = new ArrayList<String>();
//			ids.add("1010241458");
//			ids.add("1010241459");
			logger.info("time="+time+"ms");
			if(pageUtil==null)
				ajaxRst = "false";
			else
				ajaxRst = "true";
			logger.info(ajaxRst);
		} else {
			ajaxRst = "false";
		}
		return "queryOK";
	}
	public List<String> getIds() {
		return ids;
	}

	public void setIds(List<String> ids) {
		this.ids = ids;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public Map<String, String> getQueryOption() {
		return queryOption;
	}

	public void setQueryOption(Map<String, String> queryOption) {
		this.queryOption = queryOption;
	}

	public String getAjaxRst() {
		return ajaxRst;
	}

}
